package com.singzi.javaSenior.threads.basic;

import lombok.SneakyThrows;

public class DealLock {
    public static void main(String[] args) {
        StringBuffer s1 = new StringBuffer();
        StringBuffer s2 = new StringBuffer();
        new Thread(){
            @SneakyThrows
            @Override
            public void run() {
                synchronized (s1){
                    Thread.sleep(100);
                    s2.append("1");
                    s1.append("a");
                    synchronized (s2){
                        s2.append("2");
                        s1.append("b");
                    }
                }
                System.out.println(s1);
                System.out.println(s2);
            }
        }.start();
        new Thread(new Runnable(){

            @SneakyThrows
            @Override
            public void run() {
                synchronized (s2){
                    Thread.sleep(100);
                    s2.append("3");
                    s1.append("c");
                    synchronized (s1){
                        s2.append("4");
                        s1.append("d");
                    }
                }
                System.out.println(s1);
                System.out.println(s2);
            }
        }).start();


    }

}
